﻿/************************************************************************
 * SMLib - State Model Library
 * Copyright (C) 2012-2013 Ali Khalili (khalili_DOT_ir_@SIGN_gmail_DOT_com)
 * **********************************************************************
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License (LGPL) published 
 * by the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version. This program is distributed without any
 * warranty. See the GNU Lesser General Public License for more details. 
 * ***********************************************************************
 * File         :     SMLib\Interoperability\DOT\DotFormat.cs
 * Description  :      Dot Format
 * Change log   :     -1 Sep 2013:  Initial Version
 * To Do        :     this dot class is very siplistic and doesn't have many feature
 *                    of the full dot! it needs lots of improvement
 ************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace SMLib.Interoperability.DOT
{
    /// <summary>
    /// Reada DOT file format
    /// </summary>
    public class DotFormat
    {
        /// <summary>
        /// DOT Transition (include a sourse, a destination, and a label of transition)
        /// </summary>
        public class DotTransition
        {
            /// <summary>
            /// Constructor
            /// </summary>
            public DotTransition()
            {
                source = null;
                destination = null;
                label = null;
            }

            public DotTransition(string source, string destination, string label)
            {
                this.source = source;
                this.destination = destination;
                this.label = label;
            }

            public static DotTransition Parse(string dotStr)
            {
                throw new NotImplementedException();
            }

            public override string ToString()
            {
                return base.ToString();
            }

            string source;

            public string Source
            {
                get { return source; }
            }

            string destination;
            public string Destination
            {
                get { return destination; }
            }


            string label;
            public string Label
            {
                get { return label; }
            }
        }

        /// <summary>
        /// Write to a DOT file
        /// </summary>
        /// <param name="outputfile"></param>
        /// <param name="graphName"></param>
        /// <param name="transitions"></param>
        public void WriteDotFile(string outputfile, string graphName, List<DotTransition> transitions)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(graphName + " {");
            foreach (DotTransition tr in transitions)
                sb.Append(tr.ToString());
            sb.Append("}");
            System.IO.File.WriteAllText(outputfile, sb.ToString());
        }

        /// <summary>
        /// Read a DOT file
        /// </summary>
        /// <param name="inputFile">address of input file</param>
        /// <returns>Dot Transitions</returns>
        public IEnumerable<DotTransition> ReadDotFile(string inputFile)
        {
            if (!System.IO.File.Exists(inputFile))
                throw new Exception("Fatal error: the file '" + inputFile + "' does not exist!");

            //* Read the file & return line by line.
            System.IO.StreamReader dotFile = new System.IO.StreamReader(inputFile);
            int lineCount = 0;
            string line;

            while ((line = dotFile.ReadLine()) != null)
            {
                DotTransition tr;

                lineCount++;
                string src, dst, lbl;
                //* Skip the first line AND empty lines
                if (lineCount == 1 || line.Length == 0)
                    continue;
				
                //* finish with the last line
                if (line.Equals("}"))
                    break;

                int i, j;
                i = 0;
                j = line.IndexOf("->", i + 1);
                src = line.Substring(0, j - i);
				src=src.Trim();

                i = line.IndexOf("->");
                j = line.IndexOf("[");
                dst = line.Substring(i + 2, j - i - 2);
				dst=dst.Trim();
				
                i = line.IndexOf("\"");
                j = line.IndexOf("\"", i + 1);
                lbl = line.Substring(i + 1, j - i - 1);

                tr = new DotTransition(src, dst, lbl);
                yield return tr;
            }

			dotFile.Close ();
        }
    }
}
